package cn.itcast.bos.activemq;

import java.util.List;

import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.springframework.stereotype.Component;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse.SmsSendDetailDTO;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;

import cn.itcast.bos.utils.SmsDemoUtils;

@Component("queueConsumer")
public class SmsConsumer implements MessageListener {

	@Override
	public void onMessage(Message message) {
		MapMessage mapMessage = (MapMessage) message;
		try {
			// 获取电话号码
			String telephone = mapMessage.getString("telephone");
			// 获取四位随机码
			String number = mapMessage.getString("code");
			// 调用工具类发送短息
			SendSmsResponse smsResponse = SmsDemoUtils.sendSms(telephone, number);
			
			if (smsResponse.getCode().equals("OK")) {
				QuerySendDetailsResponse querySendDetails = SmsDemoUtils.querySendDetails(smsResponse.getBizId());
				// 发送成功
				System.out.println("发送短信成功,手机号：" + telephone + "，验证码：" + number);
				List<SmsSendDetailDTO> smsSendDetailDTOs = querySendDetails.getSmsSendDetailDTOs();
				for (QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : smsSendDetailDTOs) {
					System.out.println("短信内容：" + smsSendDetailDTO.getContent());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

}
